/*
 * EnergieBedrijfView.java
 */

package energiebedrijf;

import energiebedrijf.Classes.*;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.BindingGroup;

/**
 * Het hoofdformulier van de applicatie
 */
public class EnergieBedrijfView extends FrameView {
    private Manager _superManager;
    
    public EnergieBedrijfView(SingleFrameApplication app, Manager man) {
        super(app);
        initComponents();
        
        //Bewaar een referentie naar de super manager en laat de data zien
        this._superManager = man;
        RefreshData();

        //Zorg ervoor dat selecties in de tabel gevolgd worden
        masterTable.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    firePropertyChange("recordSelected", !isRecordSelected(), isRecordSelected());
                }
            });
            
        //Zorg ervoor dat selecties in de perceel tabel gevolgd worden
        detailTable.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    firePropertyChange("detailRecordSelected", !isDetailRecordSelected(), isDetailRecordSelected());
                }
            }); 
    }

    /**
     * Geeft aan of er een record geselecteerd is in de klant lijst
     * @return True als er een record geselecteerd is, anders false
     */
    public boolean isRecordSelected() {
        return masterTable.getSelectedRow() != -1;
    }
    
    /**
     * Geeft aan of er een record geselecteerd is in de perceel lijst
     * @return True als er een record geselecteerd is anders false
     */
    public boolean isDetailRecordSelected() {
        return detailTable.getSelectedRow() != -1;
    }

    /**
     * Voegt een nieuwe klant toe
     */
    @Action
    public void newRecord() {
        //Maak nieuw klant object en laat formulier zien
        energiebedrijf.Classes.Klant k = new energiebedrijf.Classes.Klant();
        
        EBKlantForm frmKlantEdit = new EBKlantForm(this.getFrame(), true, k);
        
        if (!frmKlantEdit.getCancelled()) {
            //Formulier is niet gecancelled dus opslaan
            this._superManager.getKlManager().getKlantLijst().add(k);
            this._superManager.getKlManager().saveAll();
            //SaveEntities();
            RefreshData();
        }
        
        frmKlantEdit.dispose();
    }

    /**
     * Verwijderd een of meerdere klanten
     */
    @Action(enabledProperty = "recordSelected")
    public void deleteRecord() {
        //Haal geselecteerde klanten op
        int[] selected = masterTable.getSelectedRows();
        ArrayList<Klant> toRemove = new ArrayList<Klant>(selected.length);
        for (int idx=0; idx<selected.length; idx++) {
            Klant k = klantList1.get(masterTable.convertRowIndexToModel(selected[idx]));
            toRemove.add(k);
        }
        
        if (toRemove.size() > 0) {
            if (JOptionPane.showConfirmDialog(this.getFrame(), "Weet u zeker dat u de geselecteerde klant(en) wilt verwijderen?", "Bevestigen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                //Verwijder klanten uit lijst.
                this._superManager.getKlManager().RemoveAll(toRemove);
                RefreshData();
            }

        }
    }
    
    /**
     * Voegt een nieuw perceel toe
     */
    @Action(enabledProperty = "recordSelected")
    public void newDetailRecord() {
        
        Perceel p = new Perceel();
        EBAansluitingen frmAansluitingen = new EBAansluitingen(this.getFrame(), true, p);
        frmAansluitingen.setVisible(true);
        
        if (!frmAansluitingen.getCancelled()) {
            //Save Perceel
            p.setKlant(GetSelectedKlant());
            this._superManager.getPLManger().getPerceelLijst().add(p);
            SaveEntities();
            RefreshData();
        }
    }

    /**
     * Verwijderd 1 of meer percelen
     */
    @Action(enabledProperty = "detailRecordSelected")
    public void deleteDetailRecord() {
        
        ArrayList<Perceel> toRemove = new ArrayList<Perceel>();
        toRemove.add((GetSelectedPerceel()));
        
        if (toRemove.size() > 0) {
            if (JOptionPane.showConfirmDialog(this.getFrame(), "Weet u zeker dat u de geselecteerde perce(e)l(en) wilt verwijderen?", "Bevestigen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                //Verwijder klanten uit lijst.
                this._superManager.getPLManger().RemoveAll(toRemove);
                RefreshData();
            }
        }
    }

    /**
     * Laat een scherm zien met wat algemene informatie
     */
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = EnergieBedrijfApp.getApplication().getMainFrame();
            aboutBox = new EnergieBedrijfAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        EnergieBedrijfApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        mainPanel = new javax.swing.JPanel();
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new ReadOnlyTable();
        newButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        detailScrollPane = new javax.swing.JScrollPane();
        detailTable = new ReadOnlyTable();
        deleteDetailButton = new javax.swing.JButton();
        newDetailButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnEditKlant = new javax.swing.JButton();
        btnEditPerceel = new javax.swing.JButton();
        btnMeterstand = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem newRecordMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem deleteRecordMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        javax.swing.JMenuItem refreshMenuItem = new javax.swing.JMenuItem();
        mnuKlantenOpslaan = new javax.swing.JMenuItem();
        jmnuVastrecht = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        klantList1 = new java.util.LinkedList();

        mainPanel.setName("mainPanel"); // NOI18N

        masterScrollPane.setName("masterScrollPane"); // NOI18N

        masterTable.setName("masterTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, klantList1, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${klantId}"));
        columnBinding.setColumnName("Klant Id");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${klantNaam}"));
        columnBinding.setColumnName("Klant Naam");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${straat}"));
        columnBinding.setColumnName("Straat");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${huisnummer}"));
        columnBinding.setColumnName("Huisnummer");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${postcode}"));
        columnBinding.setColumnName("Postcode");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${plaats}"));
        columnBinding.setColumnName("Plaats");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterScrollPane.setViewportView(masterTable);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(energiebedrijf.EnergieBedrijfApp.class).getContext().getResourceMap(EnergieBedrijfView.class);
        masterTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("masterTable.columnModel.title0")); // NOI18N
        masterTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("masterTable.columnModel.title1")); // NOI18N
        masterTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("masterTable.columnModel.title2")); // NOI18N
        masterTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("masterTable.columnModel.title3")); // NOI18N
        masterTable.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("masterTable.columnModel.title4")); // NOI18N
        masterTable.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("masterTable.columnModel.title5")); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(energiebedrijf.EnergieBedrijfApp.class).getContext().getActionMap(EnergieBedrijfView.class, this);
        newButton.setAction(actionMap.get("newRecord")); // NOI18N
        newButton.setText(resourceMap.getString("newButton.text")); // NOI18N
        newButton.setName("newButton"); // NOI18N
        newButton.setNextFocusableComponent(deleteButton);

        deleteButton.setAction(actionMap.get("deleteRecord")); // NOI18N
        deleteButton.setText(resourceMap.getString("deleteButton.text")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N
        deleteButton.setNextFocusableComponent(detailTable);

        detailScrollPane.setName("detailScrollPane"); // NOI18N

        detailTable.setName("detailTable"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${selectedElement.perceelCollection}");
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, eLProperty, detailTable);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${perceelId}"));
        columnBinding.setColumnName("Perceel Id");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${straatnaam}"));
        columnBinding.setColumnName("Straatnaam");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${huisnummer}"));
        columnBinding.setColumnName("Huisnummer");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${postcode}"));
        columnBinding.setColumnName("Postcode");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${plaats}"));
        columnBinding.setColumnName("Plaats");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${factuurDatum}"));
        columnBinding.setColumnName("Factuur Datum");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${meteropname}"));
        columnBinding.setColumnName("Meteropname");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${electriciteitAansluiting}"));
        columnBinding.setColumnName("Electriciteit Aansluiting");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${gasAansluiting}"));
        columnBinding.setColumnName("Gas Aansluiting");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${waterAansluiting}"));
        columnBinding.setColumnName("Water Aansluiting");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        detailScrollPane.setViewportView(detailTable);

        deleteDetailButton.setAction(actionMap.get("deleteDetailRecord")); // NOI18N
        deleteDetailButton.setText(resourceMap.getString("deleteDetailButton.text")); // NOI18N
        deleteDetailButton.setName("deleteDetailButton"); // NOI18N

        newDetailButton.setAction(actionMap.get("newDetailRecord")); // NOI18N
        newDetailButton.setText(resourceMap.getString("newDetailButton.text")); // NOI18N
        newDetailButton.setName("newDetailButton"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        btnEditKlant.setAction(actionMap.get("Click")); // NOI18N
        btnEditKlant.setText(resourceMap.getString("btnEditKlant.text")); // NOI18N
        btnEditKlant.setName("btnEditKlant"); // NOI18N

        btnEditPerceel.setAction(actionMap.get("btnEditPerceel_Click")); // NOI18N
        btnEditPerceel.setText(resourceMap.getString("btnEditPerceel.text")); // NOI18N
        btnEditPerceel.setName("btnEditPerceel"); // NOI18N

        btnMeterstand.setAction(actionMap.get("btnMeterstand_Click")); // NOI18N
        btnMeterstand.setText(resourceMap.getString("btnMeterstand.text")); // NOI18N
        btnMeterstand.setName("btnMeterstand"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(detailScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)
                    .addComponent(masterScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 255, Short.MAX_VALUE)
                        .addComponent(newButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditKlant, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteButton))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(btnMeterstand)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 240, Short.MAX_VALUE)
                        .addComponent(newDetailButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditPerceel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteDetailButton))
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );

        mainPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnEditKlant, deleteButton, newButton});

        mainPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnEditPerceel, deleteDetailButton, newDetailButton});

        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(masterScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(deleteButton)
                        .addComponent(newButton)
                        .addComponent(btnEditKlant, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(detailScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deleteDetailButton)
                    .addComponent(btnEditPerceel)
                    .addComponent(newDetailButton)
                    .addComponent(btnMeterstand))
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setAction(actionMap.get("fmKlantenOpslaan_Click")); // NOI18N
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        newRecordMenuItem.setAction(actionMap.get("newRecord")); // NOI18N
        newRecordMenuItem.setText(resourceMap.getString("newRecordMenuItem.text")); // NOI18N
        newRecordMenuItem.setName("newRecordMenuItem"); // NOI18N
        fileMenu.add(newRecordMenuItem);

        deleteRecordMenuItem.setAction(actionMap.get("deleteRecord")); // NOI18N
        deleteRecordMenuItem.setText(resourceMap.getString("deleteRecordMenuItem.text")); // NOI18N
        deleteRecordMenuItem.setName("deleteRecordMenuItem"); // NOI18N
        fileMenu.add(deleteRecordMenuItem);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        refreshMenuItem.setAction(actionMap.get("mnuFactuur_Click")); // NOI18N
        refreshMenuItem.setText(resourceMap.getString("refreshMenuItem.text")); // NOI18N
        refreshMenuItem.setName("refreshMenuItem"); // NOI18N
        fileMenu.add(refreshMenuItem);

        mnuKlantenOpslaan.setAction(actionMap.get("fmKlantenOpslaan_Click")); // NOI18N
        mnuKlantenOpslaan.setText(resourceMap.getString("mnuKlantenOpslaan.text")); // NOI18N
        mnuKlantenOpslaan.setName("mnuKlantenOpslaan"); // NOI18N
        fileMenu.add(mnuKlantenOpslaan);

        jmnuVastrecht.setAction(actionMap.get("jmnuVastrecht_Click")); // NOI18N
        jmnuVastrecht.setName("jmnuVastrecht"); // NOI18N
        fileMenu.add(jmnuVastrecht);

        jSeparator2.setName("jSeparator2"); // NOI18N
        fileMenu.add(jSeparator2);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 674, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 504, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Wijzig een bestaande klant
     */
    @Action(enabledProperty = "recordSelected")
    public void Click() {
        //Haal de geselecteerde klant index op
        int idxParent = masterTable.getSelectedRow();
        
        energiebedrijf.Classes.Klant klant = klantList1.get(masterTable.convertRowIndexToModel(idxParent));
        EBKlantForm frmEditKlant = new EBKlantForm(this.getFrame(), true, klant);
        
        if (!frmEditKlant.getCancelled()) {
            //Opslaan
            SaveEntities();
            RefreshData();
        }
        
        frmEditKlant.dispose();
    }

    /**
     * Wijzig een bestaand perceel
     */
    @Action(enabledProperty = "detailRecordSelected")
    public void btnEditPerceel_Click() {
        Perceel p = GetSelectedPerceel();
        
        //Laat het Edit formulier zien
        EBAansluitingen frmAansluitingen = new EBAansluitingen(this.getFrame(), true, p);
        frmAansluitingen.setVisible(true);
        
        //Save de wijzigingen
        if (p.getGas() != null && p.getGas().getId() == -1) {
            this._superManager.getAlManager().getGasLijst().add(p.getGas());
        }
        
        if (p.getWater() != null && p.getWater().getId() == -1) {
            this._superManager.getAlManager().getWaterLijst().add(p.getWater());
        }
        
        if (p.getElectriciteit() != null && p.getElectriciteit().getId() == -1) {
            this._superManager.getAlManager().getElecLijst().add(p.getElectriciteit());
        }
        
        SaveEntities();
        RefreshData();
    }

    /**
     * Laat het scherm zien om de klanten in een bestand op te slaan
     */
    @Action
    public void fmKlantenOpslaan_Click() {
        EBKlantenBestand frmKlantenOpslaan = new EBKlantenBestand(this.getFrame(), true, klantList1);
        frmKlantenOpslaan.setVisible(true);
    }

    /**
     * Neem de meterstanden op voor het geselecteerde perceel
     */
    @Action(enabledProperty = "detailRecordSelected")
    public void btnMeterstand_Click() {
        Perceel p = GetSelectedPerceel();
        
        //Laat meterstand form zien
        EBMeterstand frmMeterstand = new EBMeterstand(this.getFrame(), true, p);
        frmMeterstand.setVisible(true);
        
        //Save perceel
        if (!frmMeterstand.isCancel()) {
            p.setMeteropname(new Date());
            SaveEntities();
            RefreshData();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditKlant;
    private javax.swing.JButton btnEditPerceel;
    private javax.swing.JButton btnMeterstand;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton deleteDetailButton;
    private javax.swing.JScrollPane detailScrollPane;
    private javax.swing.JTable detailTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JMenuItem jmnuVastrecht;
    private java.util.List<energiebedrijf.Classes.Klant> klantList1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem mnuKlantenOpslaan;
    private javax.swing.JButton newButton;
    private javax.swing.JButton newDetailButton;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private JDialog aboutBox;
    
    /**
     * Een functie om het geselecteerde perceel op te halen
     * @return Het geselecteerde perceel
     */
    private Perceel GetSelectedPerceel() {
        //Haal de geselecteerde klant en perceel op uit de jTable
        Klant klant = GetSelectedKlant();
        int idxPerceel = detailTable.convertRowIndexToModel(detailTable.getSelectedRow());
        Collection<Perceel> percelen = klant.getPerceelCollection();
        
        //Loop over de percelen heen en vind het geselecteerde perceel
        int count = 0;
        Iterator<Perceel> iter = percelen.iterator();
        while (count++ < idxPerceel) iter.next();
        Perceel p = iter.next();
        
        return p;
    }
    
    /**
     * Een functie om de geselecteerde klant op te halen
     * @return De geselecteerde klant
     */
    private Klant GetSelectedKlant() {
        //Haal de geselecteerde Klant object op
        int idxParent = masterTable.getSelectedRow();
        energiebedrijf.Classes.Klant klant = klantList1.get(masterTable.convertRowIndexToModel(idxParent));
        return klant;
    }
    
    /**
     * Sla alle gegevens op
     */
    private void SaveEntities() {
        this._superManager.SaveAll();
    }
    
    private void RefreshData() {        
        Binding b = findBinding(bindingGroup, klantList1, masterTable);
        b.unbind();

        klantList1.clear();
        this._superManager.RefreshData();
        
        for (Klant k : _superManager.getKlManager().getKlantLijst()) {
            klantList1.add(k);
        }
        
        b.bind();
        masterTable.repaint();
        
        this.detailTable.removeAll();
        this.detailTable.repaint();
    }

    /**
     * Laat factuur overzicht zien en sla daarna op
     */
    @Action
    public void mnuFactuur_Click() {
        EBFactuurOverzicht frmFactuur = new EBFactuurOverzicht(this.getFrame(), true, klantList1);
        frmFactuur.setVisible(true);
        SaveEntities();
        RefreshData();
        frmFactuur.dispose();
    }
    
    /**
     * Functie voor ondersteuning van het verversen van de data.
     * @param bindingGroup
     * @param source
     * @param target
     * @return
     */
    private Binding findBinding(BindingGroup bindingGroup, Object source, Object target) {
        for (Iterator<Binding> i = bindingGroup.getBindings().iterator(); i.hasNext();) {
            Binding b = i.next();
            boolean found = (source == null || b.getSourceObject() == source) &&
                    (target == null || b.getTargetObject() == target);
            
            if (found) {
                return b;
            }
        }
        
        return null;
    }

    /**
     * Laat het menu zien om het vastrecht mee aan te passen.
     */
    @Action
    public void jmnuVastrecht_Click() {
        EBVastrecht frmVastrecht = new EBVastrecht(this.getFrame(), true);
        frmVastrecht.setVisible(true);
    }
}
